/**
 * 
 */
package com.zhiteng.service.impl;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.zhiteng.entity.CourseTime;
import com.zhiteng.mapper.CourseTimeMapper;
import com.zhiteng.service.CourseTimeService;
import com.zhiteng.utils.common.PoiUtils;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**  
* <p>Title: CourseTimeServiceImpl.java</p>  
* <p>Description: 上课时间表Service</p>  
* @author dragon  
* @date 2018年12月10日  
* @version 1.0  
*/
@Service
public class CourseTimeServiceImpl implements CourseTimeService {
	@Autowired
	private CourseTimeMapper courseTimeMapper;

	@SuppressWarnings("unused")
	@Override
	public int saveCourseTimeByExcel(MultipartFile file, String schoolId) throws Exception{
		POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream());
		HSSFWorkbook wb=new HSSFWorkbook(fs);
		HSSFSheet hssfSheet=wb.getSheetAt(0);  	//	获取第一个Sheet页
		List<CourseTime> courseTimeList = new ArrayList<>();
		if (hssfSheet != null) {
			//	遍历单元格(一列一列的读)
			for(int i=1;i<=10;i++) { //	行
				HSSFCell c= hssfSheet.getRow(i).getCell(1);
				if(c!=null && StringUtils.isNoneBlank(PoiUtils.getValue(c))) {
					CourseTime courseTime = new CourseTime();
					courseTime.setSchool_id(schoolId);
					courseTime.setCourseNum(i);
					for(int j=1;j<=2;j++){	//	列	
						//	获取行号i、列号j的单元格
						String timeStr = PoiUtils.getValue(hssfSheet.getRow(i).getCell(j));
						timeStr = timeStr.replace(":",".");	//	将10:20转为double型的10.20，从而便于比较
						if(j==1) {
							courseTime.setStart(Double.parseDouble(timeStr));
						} else {
							courseTime.setEnd(Double.parseDouble(timeStr));
						}
					}
					courseTimeList.add(courseTime);
				}
			}
		}
		wb.close();
		int count = saveCourseTimeBatch(courseTimeList);
		return courseTimeList.size()-count;
	}
	
	
	private int saveCourseTimeBatch(List<CourseTime> courseTimeList) {
		int count = 0;
		for (CourseTime courseTime : courseTimeList) {
			courseTimeMapper.insert(courseTime);
			count++;
		}
		return count;
	}


	@Override
	public List<CourseTime> queryCourseTimeBySchoolId(String schoolId) {
		Example example = new Example(CourseTime.class);
		example.orderBy("courseNum").asc();	//	按节次升序排序
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("school_id", schoolId);
		return courseTimeMapper.selectByExample(example);
	}


	@Override
	public void deleteCourseTimeBySchoolId(String schoolId) {
		CourseTime record = new CourseTime();
		record.setSchool_id(schoolId);
		courseTimeMapper.delete(record);
	}
}
